﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Drawing
{
    /// <summary>
    /// HitTest.xaml 的交互逻辑
    /// </summary>
    public partial class HitTest : Window
    {
        public HitTest()
        {
            InitializeComponent();
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            DrawingVisual v = new DrawingVisual();
            DrawSquare(v, new Point(10, 10), false);
            AddVisual(v);
        }
        // Drawing constants.
        private Brush drawingBrush = Brushes.AliceBlue;
        private Brush selectedDrawingBrush = Brushes.LightGoldenrodYellow;
        private Pen drawingPen = new Pen(Brushes.SteelBlue, 3);
        private Size squareSize = new Size(30, 30);
        private DrawingVisual selectionSquare;

        // Rendering the square.
        private void DrawSquare(DrawingVisual visual, Point topLeftCorner, bool isSelected)
        {
            using (DrawingContext dc = visual.RenderOpen())
            {
                Brush brush = drawingBrush;
                if (isSelected) brush = selectedDrawingBrush;

                dc.DrawRectangle(brush, drawingPen,
                    new Rect(topLeftCorner, squareSize));
            }
        }
        public void AddVisual(Visual visual)
        {
            base.AddVisualChild(visual);
            base.AddLogicalChild(visual);
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DrawingVisual v = new DrawingVisual();
            DrawSquare(v, new Point(10, 10), false);
            AddVisual(v);
        }

        private void drawingSurface_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Point pointClicked = e.GetPosition(drawingSurface);
            DrawingVisual visual = new DrawingVisual();
            DrawSquare(visual, pointClicked, false);
            drawingSurface.AddVisual(visual);
        }

        private void drawingSurface_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {

        }

        private void drawingSurface_MouseMove(object sender, MouseEventArgs e)
        {

        }
    }
}
